<?php

namespace app\admin\controller;

use app\model\LessonModel;
use think\Db;
use think\Validate;

class Lesson extends Base
{
    /**
     * 课程列表页面
     */
    public function indexLesson()
    {
        return $this->fetch('lesson/indexLesson');
    }

    /**
     * 课程列表接口
     */
    public function getLessonList()
    {
        $param=$this->request->param();

        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;

        $LessonModel = new LessonModel();
        $arr = ['like'=>'course.title'];
        $where = $this->select_param($arr,$param);

        $list = $LessonModel->getLessonListPage($page,$pageSize,$where,false);
        $count = $LessonModel->getLessonListPage(0,0,$where,true);
        return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
    }

    public function changeLesson()
    {
        $param = $this->request->param();
        if(!empty($param['c_id'])){
            $info = Db::table('course')->where('c_id',$param['c_id'])->find();
            $this->assign('info',$info);
        }
        return $this->fetch('lesson/changeLesson');
    }



    /**
     * 添加修改
     */
    public function updateLesson()
    {
        $param = $this->request->param();
        $rule = [
            'title|课程名称'=>'require',
            'type|课程类型'=>'require',
            'status|状态'=>'require',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            return  json(['code'=>400,'msg'=>$validate->getError()]);
        }

        $data = [
            'title'=>$param['title'],
            'type'=>$param['type'],
            'status'=>$param['status'],
        ];

        if(!empty($param['c_id'])){
            $data['updated_at'] = afterTime();
            $info = Db::table('course')->where('c_id',$param['c_id'])->update($data);
        }else{
            $data['admin_id'] = $this->uid;
            $data['created_at'] = afterTime();
            $data['delete_flag'] = 1;
            $info = Db::table('course')->insert($data);
        }
        if($info){
            return json(['code' => 200,'msg'=>'操作成功']);
        }else{
            return json(['code' => 400,'msg'=>'操作失败']);
        }

    }

    /**
     * 删除课程
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delLesson()
    {
        $param=$this->request->param();
        $rule = [
            'c_id'=>'require|number',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            return json(['code' => 400,'msg'=>$validate->getError()]);
        }
        $res = Db::table('course')->where('c_id',$param['c_id'])->update(['delete_flag'=>2,'updated_at'=>date('Y-m-d H:i:s',time())]);
        if($res){
            return json(['code' => 200,'msg'=>'删除成功']);

        }else{
            return json(['code' => 400,'msg'=>'删除失败']);
        }
    }

    /**
     * 课程开启关闭
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function changeLessonStatus()
    {
        $param=$this->request->param();
        $rule = [
            'c_id'=>'require|number',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            return json(['code' => 400,'msg'=>$validate->getError()]);
        }
        $res = Db::table('course')->where('c_id',$param['c_id'])->update(['status'=>2,'updated_at'=>date('Y-m-d H:i:s',time())]);
        if($res){
            return json(['code' => 200,'msg'=>'操作成功']);
        }else{
            return json(['code' => 400,'msg'=>'操作失败']);
        }
    }



}